package cn.xtxfan.infrastructure.persistent.repository;

import cn.xtxfan.domain.plan.model.dto.CheckAddPlanDTO;
import cn.xtxfan.domain.plan.model.dto.CheckPlanDTO;
import cn.xtxfan.domain.plan.model.entity.CheckPartnerEntity;
import cn.xtxfan.domain.plan.model.entity.CheckPlanEntity;
import cn.xtxfan.domain.plan.model.valobj.CheckPlanVO;
import cn.xtxfan.domain.plan.repository.IPlanRepository;
import cn.xtxfan.infrastructure.persistent.dao.ICheckPartnerDao;
import cn.xtxfan.infrastructure.persistent.dao.ICheckPlanDao;
import cn.xtxfan.infrastructure.persistent.dao.ICheckUserDao;
import cn.xtxfan.infrastructure.persistent.po.CheckPlan;
import cn.xtxfan.infrastructure.persistent.po.CheckUser;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @className: PlanRepository
 * @author: XTX
 * @date: 2025/7/19 20:52
 * @Version: 1.0
 * @description:
 */
@Repository
public class PlanRepository implements IPlanRepository {

    @Resource
    private ICheckUserDao userDao;

    @Resource
    private ICheckPlanDao planDao;

    @Resource
    private ICheckPartnerDao partnerDao;

    @Override
    @Transactional
    public CheckPlanVO queryUserPartnerPlans(String userId) {
        List<String> userIds = new ArrayList<>(2);
        userIds.add(userId);
        // 查询partnerId
        String partnerId = partnerDao.queryPartnerIdByUserId(userId);
        if (!partnerId.isEmpty()) {
            userIds.add(partnerId);
        }

        List<CheckPlan> checkPlans = planDao.queryCheckPlanByUserIds(userIds);
        List<CheckPlanEntity> checkPlanEntities = new ArrayList<>(checkPlans.size());
        for (CheckPlan checkPlan : checkPlans) {
            CheckPlanEntity checkPlanEntity = CheckPlanEntity.builder()
                        .planId(checkPlan.getPlanId())
                        .plan(checkPlan.getPlan())
                        .isFinish(checkPlan.getIsFinish())
                        .userId(checkPlan.getUserId())
                        .build();
            checkPlanEntities.add(checkPlanEntity);
        }

        List<CheckPlanDTO> userFinishList = new ArrayList<>();
        List<CheckPlanDTO> userUnFinishList = new ArrayList<>();
        List<CheckPlanDTO> partnerFinishList = new ArrayList<>();
        List<CheckPlanDTO> partnerUnFinishList = new ArrayList<>();

        for (CheckPlanEntity checkPlanEntity : checkPlanEntities) {
            CheckPlanDTO checkPlanDTO = CheckPlanDTO.builder()
                        .planId(checkPlanEntity.getPlanId())
                        .plan(checkPlanEntity.getPlan())
                        .build();
            if (checkPlanEntity.getUserId().equals(userId) && checkPlanEntity.getIsFinish() == 1) {
                userFinishList.add(checkPlanDTO);
            } else if (checkPlanEntity.getUserId().equals(userId) && checkPlanEntity.getIsFinish() == 0) {
                userUnFinishList.add(checkPlanDTO);
            } else if (!checkPlanEntity.getUserId().equals(userId) && checkPlanEntity.getIsFinish() == 1) {
                partnerFinishList.add(checkPlanDTO);
            } else if (!checkPlanEntity.getUserId().equals(userId) && checkPlanEntity.getIsFinish() == 0) {
                partnerUnFinishList.add(checkPlanDTO);
            }
        }
        CheckUser checkUser = new CheckUser();
        checkUser.setUserId(userId);
        CheckUser userCheck = userDao.queryCheckUser(checkUser);
        checkUser.setUserId(partnerId);
        CheckUser partnerCheck = userDao.queryCheckUser(checkUser);

        return CheckPlanVO.builder()
                .userFinishList(userFinishList)
                .userUnFinishList(userUnFinishList)
                .partnerFinishList(partnerFinishList)
                .partnerUnFinishList(partnerUnFinishList)
                .nickname(userCheck.getNickname())
                .partnerNickname(partnerCheck.getNickname())
                .build();


    }

    @Override
    public void finishPlan(String planId) {
        planDao.finishPlanByPlanId(planId);
    }

    @Override
    public void delPlan(String planId) {
        planDao.delPlanByPlanId(planId);
    }

    @Override
    public void addPlan(CheckAddPlanDTO checkAddPlanDTO) {
        CheckPlan checkPlan = new CheckPlan();
        checkPlan.setPlan(checkAddPlanDTO.getPlan());
        checkPlan.setUserId(checkAddPlanDTO.getUserId());
        String planId = UUID.randomUUID().toString().replaceAll("-", "");
        checkPlan.setPlanId(planId);
        planDao.insert(checkPlan);
    }
}

